<?php 
Yii::import('system.web.widgets.CWidget');
class CCroppicDialog extends CWidget{
    
    public $croppic;
    public $title;
    
    public function run(){
        $this->registerClientScript()->registerClientCss();
        $this->renderWidgetBody();
    }
    
    
    public function renderWidgetBody(){
        
        $this->croppic['imgurlinputId'] = $this->getId().'-croopic-dialog-input';
        
        
        $this->beginWidget('application.components.widgets.CDialog',array(
            'id' 				=> $this->getId(),
            'title'				=> $this->title,
            'buttons'	=>array(
                'useimg'    => array(
                    'title' => 'Use Image',
                    'click' => 'js:function(){
                        if($("#'.$this->croppic['imgurlinputId'].'").val() == ""){ 
                            $("#'.$this->getId().'-croopic-dialog-message").show();
                        }else{ 
                            $("#'.$this->getId().'-croopic-dialog-message").hide();
                            var _thumbnailId = $("#'.$this->getId().'").attr("item-thumbnail");
                            var _imgurl = $("#'.$this->croppic['imgurlinputId'].'").val();
                            $("#"+_thumbnailId).find(".item-thumbnail-url").val(_imgurl);
                            $("#"+_thumbnailId).find(".img-thumbnail").attr("src",_imgurl);
                            $("#'.$this->getId().'").modal("hide");
                        }
                    }'
                ),
                'close'=>array(
                	'type'	=>  'close',
                ),
            ),
        ));
        echo '
        <div id="'.$this->getId().'-croopic-dialog-message" class="alert alert-danger alert-dismissible fade in" role="alert">
            <strong>Error</strong>
        </div>';
        
        $this->croppic['jsOptions'] = array(
            'onBeforeImgUpload' => 'js:function(){ 
                $("'.$this->getId().'-croopic-dialog-message").hide();
            }',
        );
        
        $this->widget('ext.croppic.widgets.Croppic',$this->croppic);
        
        $this->endWidget();
        
    }
    
    public function registerClientScript(){
        
        if (!array_key_exists('id',$this->croppic)){
            $this->croppic['id'] = $this->getId().'-croppic';
        }        
        
        Yii::app()->clientScript->registerScript($this->getId().'-imgurlinputId-js',"
            $('#{$this->getId()}').on('hide.bs.modal',function(){
                 $('#{$this->getId()}-croopic-dialog-message').hide();
            });
            $('#{$this->getId()}-croopic-dialog-message').hide();
            $('#{$this->getId()}').on('show.bs.modal', function (event) {
                $('#{$this->croppic['id']}').croppic('overload');
            });
            
        ");
        
        return $this;
    }
    
    public function registerClientCss(){
        
        if (!array_key_exists('id',$this->croppic)){
            $this->croppic['id'] = $this->getId().'-croppic';
        }
        
        Yii::app()->clientScript->registerCss($this->getId().'-croopic-dialog-css',"
            #{$this->croppic['id']}{
               margin:15px auto; 
	           border: 1px solid #c1c1c1;
            }
        ");
        
        return $this;
    }
}